﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using System.Configuration;
using Ext.Net;
using System.Data;
using Newtonsoft.Json;
using System.IO;
using System.Text;


namespace TCDC_System.Content.MasterData
{
    public partial class p_tcdc_trn_user_campaign : SystemPage.UIPage
    {
        private tcdc_trn_user_campaign objUserCampaign;
        protected void Page_Init(object sender, EventArgs e)
        {
            this.UserCampaignAdd1.GridStore = StoreUserCampaign;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            objUserCampaign = new tcdc_trn_user_campaign();
            objUserCampaign.strConn = ConfigurationManager.AppSettings["ConnString"];
            if (!X.IsAjaxRequest)
            {
                StoreUserCampaign.DataSource = GetUserCampaign();
                StoreUserCampaign.DataBind();
            }
        }

        protected DataTable GetUserCampaign()
        {
            DataTable dt = objUserCampaign.getByObject(new tcdc_trn_user_campaign());
            return dt;
        }

        protected void AddUserCampaign(object sender, DirectEventArgs e)
        {
            UserCampaignAdd1.Show();
        }

        protected void UpdateUserCampaign(object sender, DirectEventArgs e)
        {
            string json = e.ExtraParams["UserCampaignValue"];
            Dictionary<string, string>[] workingdays = JSON.Deserialize<Dictionary<string, string>[]>(json);
            string user_campaign_id = string.Empty;
            string user_id = string.Empty;
            string product_id = string.Empty;
            string campaign_id = string.Empty;
            string member_type_id = string.Empty;
            string payment_detail_id = string.Empty;
            string barcode = string.Empty;
            string start_date = string.Empty;
            string expire_date = string.Empty;
            string status = string.Empty;
            string create_date = string.Empty;
            string create_by = string.Empty;
            string update_date = string.Empty;
            string update_by = string.Empty;

            foreach (Dictionary<string, string> row in workingdays)
            {
                user_campaign_id = row["user_campaign_id"].ToString();
                user_id = row["user_id"].ToString();
                product_id = row["product_id"].ToString();
                campaign_id = row["campaign_id"].ToString();
                member_type_id = row["member_type_id"].ToString();
                payment_detail_id = row["payment_detail_id"].ToString();
                barcode = row["barcode"].ToString();
                start_date = row["start_date"].ToString();
                expire_date = row["expire_date"].ToString();
                status = row["status"].ToString();
                create_date = row["create_date"].ToString();
                create_by = row["create_by"].ToString();
                update_date = row["update_date"].ToString();
                update_by = row["update_by"].ToString();
            }

            objUserCampaign.user_campaign_id = Convert.ToInt32(user_campaign_id);
            objUserCampaign.user_id = Convert.ToInt32(user_id);
            objUserCampaign.product_id = Convert.ToInt32(product_id);
            objUserCampaign.campaign_id = Convert.ToInt32(campaign_id);
            objUserCampaign.member_type_id = Convert.ToInt32(member_type_id);
            objUserCampaign.payment_detail_id = Convert.ToInt32(payment_detail_id);
            objUserCampaign.barcode = barcode;
            objUserCampaign.start_date = start_date;
            objUserCampaign.expire_date = expire_date;
            objUserCampaign.status = status;
            objUserCampaign.create_date = create_date;
            objUserCampaign.create_by = Convert.ToInt32(create_by);
            objUserCampaign.update_date = update_date;
            objUserCampaign.update_by = Convert.ToInt32(update_by);
            objUserCampaign.updateData(objUserCampaign);
            StoreUserCampaign.DataSource = GetUserCampaign();
            StoreUserCampaign.DataBind();
        }

        protected void DeleteUserCampaign(object sender, DirectEventArgs e)
        {
            this.ConfirmDeleteUserCampaign();
        }

        protected void StoreUserCampaign_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            StoreUserCampaign.DataSource = GetUserCampaign();
            StoreUserCampaign.DataBind();
        }

        protected void ExportExcelUserCampaign(object sender, EventArgs e)
        {
            string json = GridData.Value.ToString();

            DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));

            if (dt.Rows.Count > 0)
            {
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=UserCampaign.xls");
                Response.ContentType = "application/vnd.ms-excel";
                StringWriter stringWriter = new StringWriter();
                HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
                DataGrid dataExportExcel = new DataGrid();
                dataExportExcel.DataSource = dt;
                dataExportExcel.DataBind();
                dataExportExcel.RenderControl(htmlWrite);
                StringBuilder sbResponseString = new StringBuilder();
                sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head> <body>");
                sbResponseString.Append(stringWriter + "</body></html>");
                sbResponseString.Replace("&nbsp;", "&#160;");
                Response.Write(sbResponseString.ToString());
                Response.End();
            }
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void ConfirmDeleteUserCampaign()
        {
            X.Msg.Confirm("Confirm", "ยืนยันการลบ ?", new MessageBoxButtonsConfig
            {
                Yes = new MessageBoxButtonConfig
                {
                    Handler = "MyNameSpace.DeleteDataUserCampaign()",
                    Text = "Yes"
                },
                No = new MessageBoxButtonConfig { Text = "No" }
            }).Show();
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void DeleteDataUserCampaign()
        {
            RowSelectionModel sm = this.GridPanelUserCampaign.SelectionModel.Primary as RowSelectionModel;
            string user_campaign_id = string.Empty;

            foreach (SelectedRow row in sm.SelectedRows)
            {
                user_campaign_id = row.RecordID;
            }
            objUserCampaign.deleteData(user_campaign_id);
            StoreUserCampaign.DataSource = GetUserCampaign();
            StoreUserCampaign.DataBind();
        }
    }
}

